package io.renren.modules.sys.web_api.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import io.renren.annotation.Login;
import io.renren.annotation.LoginUser;
import io.renren.common.exception.RRException;
import io.renren.common.utils.R;
import io.renren.modules.sys.entity.TokenEntity;
import io.renren.modules.sys.entity.UserEntity;
import io.renren.modules.sys.lucene.entity.User;
import io.renren.modules.sys.service.TokenService;
import io.renren.modules.sys.service.UserService;
import org.apache.commons.codec.digest.DigestUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/api/users")
public class WebUserController {
    @Autowired
    private TokenService tokenService;
    @Autowired
    private UserService userService;

    @RequestMapping("/reg")
    public String jrreg(){
        return "webpage/register";
    }
    @ResponseBody
    @RequestMapping("/register")
    public R register(String name,String passw){
        //表单校验
        if(name==null||name.equals("")){
            return R .error("请填写用户名！");
        }
        if(passw==null||passw.equals("")){
            return R .error("请填写密码！");
        }
        UserEntity olduser = userService.selectOne(new EntityWrapper<UserEntity>().eq("username",name));
        if(olduser!=null){
            return R .error("用户名已存在");
        }
        UserEntity user = new UserEntity();
        user.setUsername(name);
        user.setPassword(DigestUtils.sha256Hex(passw));
        user.setCreateTime(new Date());
        user.setMobile("13535374265");
        userService.insert(user);

        return R.ok();
    }
    @RequestMapping("/log")
    public String log(){
        return "webpage/login";
    }
    @ResponseBody
    @RequestMapping("/login")
    public R UserLogin(String name,String passw){
        //获取登录token
        UserEntity loguser = userService.selectOne(new EntityWrapper<UserEntity>().eq("username",name));
        if(loguser==null){
            return R.error("用户名不存在！");
        }else{
            //密码错误
            if(!loguser.getPassword().equals(DigestUtils.sha256Hex(passw))){
                return R.error("密码错误");
            }
        }
        TokenEntity tokenEntity = tokenService.createToken(loguser.getUserId());
        Map<String, Object> map = new HashMap<>(2);
        map.put("token", tokenEntity.getToken());
        map.put("expire", tokenEntity.getExpireTime().getTime() - System.currentTimeMillis());
        return R.ok(map);
    }
    @Login
    @RequestMapping("/logout")
    public R logout( @RequestAttribute("userId") long userId){
        tokenService.expireToken(userId);
        return R.ok();
    }

}
